package com.xbq.oauth.controller;


import com.xbq.oauth.dao.MemberMapper;
import com.xbq.oauth.model.Member;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private MemberMapper memberMapper;

    @GetMapping("/hello")
    public String hello(){
        return "member_hello";
    }

    /**
     * 获取SpringSecurity中存储的用户信息
     * @param user
     * @return
     */
    @GetMapping()
    public Object getCurrentUser(@AuthenticationPrincipal UserDetails user){
        return user;
    }


    @PostMapping("/add")
    public String addMember(@RequestBody Member member){
        log.info("用户名为：{}", member.getUsername());
        log.info("密码为：{}",member.getPassword());
        String encodePwd = passwordEncoder.encode(member.getPassword());
        log.info("加密后的密码为：{}",encodePwd);
        member.setPassword(encodePwd);
        memberMapper.insert(member);
        return member.getId().toString();
    }

    /*@PostMapping("/login")
    public String login(@RequestBody Member member){


        return null;
    }*/
}
